angular.module("crawlerApp").controller("simpleConfigCtrl", function ($scope, $http, $state, $rootScope) {
    $scope.configListNew = [];
    $scope.hasQuartz = false;
    getConfigList("all");
    $scope.type = "all";
    $scope.parseConfig = {
        customSelect: '',
        selectDescribe: ''
    };

    $scope.newConfig = {
        seedDescribe: '',
        catalog: '',
        seedUrl: '',
        nextButton: '',
        contentElement: '',
    };

    $scope.getConfigList = function (type) {
        $scope.type = type;
        getConfigList(type)
    };

    function getConfigList(type) {
        $http.get("crawler/config/getByType/" + type).then(function (response) {
            $scope.configList = response.data;
            getCawlerUrl();
            getTaskByStatus();
            getStatusByRuleId();

        });
    }


    function getCawlerUrl() {
        $scope.configListNew = [];
        //getQuartz();

        $http.get("crawler/config/getCawlerUrl").then(function (response) {
            $scope.cawlerUrlList = response.data;

            angular.forEach($scope.configList, function (data) {

                data.num = $scope.cawlerUrlList[data.id];

                $scope.configListNew.push(data);
            })
        });
        //getStatusByRuleId();

    }

    function getStatusByRuleId() {
        $scope.configListStatus = [];
        //  angular.forEach(configList,function (data) {
        $http.get("crawler/task/getAllTask").then(function (response) {
            $scope.taskStatusList = response.data;

            angular.forEach($scope.configList, function (item) {
                angular.forEach($scope.taskStatusList, function (data) {
                    if (data.ruleId === item.id) {
                        item.status = data.status;
                    }
                });
                $scope.configListStatus.push(item);
            });

        })
    };

    /*    function getQuartz() {
            $http.get("crawler/config/getQuartz").then(function (response) {
                $scope.quartzList=response.data;

            });

        }*/

    $scope.detailData = function (data) {
        $scope.sourceResult = data;
        $scope.updateData = angular.copy(data);
        $http.get("crawler/config/find/" + data.id).then(function (response) {
            $scope.customResult = response.data;
        })
    };

    $scope.confirmAdd = function (data) {
        $http.put("crawler/config/add", data).then(function (response) {
            //$scope.sourceResult = response.data;
            if (response.status == '200') {
                alert("添加成功！");
            }
            getConfigList($scope.type);

        });

    };
    $scope.confirm = function () {
        // todo 修改之后的结果

        $http.put("crawler/config/saveOrUpdate", $scope.updateData).then(function (response) {
            $scope.sourceResult = response.data;
            updateParse();
            getConfigList($scope.type);
        });

    };

    function updateParse() {
        $http.put("crawler/config/updateParse", $scope.customResult).then(function (response) {
            // $scope.sourceResult = response.data;
            getConfigList($scope.type)
        })
    }

    $scope.delete = function (config) {
        $scope.dataToDelete = angular.copy(config);
    };
    $scope.newRules = function () {
        $scope.newConfig = {
            seedDescribe: '',
            catalog: '',
            seedUrl: '',
            nextButton: '',
            contentElement: '',
        };
    };

    $scope.parse = function (config) {
        $scope.parseConfig = {
            customSelect: '',
            selectDescribe: ''
        };
        $scope.newData = angular.copy(config);
    };
    $scope.commitParse = function (data) {
        $scope.parseData = angular.copy(data);
        $scope.parseData.ruleId = angular.copy($scope.newData.id);
        $http.put("crawler/config/parseAdd", $scope.parseData).then(function (response) {
            //$scope.sourceResult = response.data;
            getConfigList($scope.type)
        });

    };
    $scope.confirmDelete = function () {
        $http.delete("crawler/config/delete/" + $scope.dataToDelete.id).then(function (response) {
            // $scope.sourceResult = response.data;
            if (response.status == '200') {
                alert("删除成功！");
            }
            getConfigList($scope.type);

        })
    };

    $scope.deleteParse = function (data) {
        $http.delete("crawler/config/deleteParse/" + data.id).then(function (response) {
            // $scope.sourceResult = response.data;
            if (response.status == '200') {
                alert("删除成功！");
            }
            getConfigList($scope.type)
        })

    };

    $scope.goDetail = function (config) {
        $state.go("detail", {config: config});
    };
    $scope.config = {
        ruleId: "",
        cron: ""
    };
    $scope.addQuartz = function (config) {
        $scope.config.ruleId = config.id;
    };
    $scope.addQuartzSure = function (config) {

        $http.put("crawler/quartz/add", config).then(function (response) {
            if (response.status == "200") {
                alert("设置成功!");
            }
        })

    };
    $scope.modifyQuartz = function (config) {
        $scope.config.ruleId = config.id;

    };
    $scope.modifyQuartzSure = function (config) {
        $http.post("crawler/quartz/modify", config).then(function (response) {
            if (response.status == "200") {
                alert("修改成功!");
            }
        })
    };
    $scope.cancelQuartz = function (config) {
        $scope.dataToCancel = angular.copy(config);
    };

    $scope.cancelQuartzSure = function () {
        $http.post("crawler/quartz/cancel", $scope.dataToCancel).then(function (response) {
            if (response.status == "200") {
                alert("取消成功!");
            }
        })
    };


    $scope.isChecked = function (newConfig) {
        if (newConfig.checked == true) {
            newConfig.checked = false;
        } else {
            newConfig.checked = true;
        }

    };
    $scope.singleTask = function (config) {

        $rootScope.data = config;
        $state.go("singleTask", {config: config});
    };

    getTaskByStatus();

    function getTaskByStatus() {
        $http.get("crawler/task/getTaskByStatus").then(function (response) {
            $scope.taskStatus = response.data;
            $scope.taskStatus.count = $scope.taskStatus.INIT + $scope.taskStatus.STOP;
        })
    }

});